$(function () {
    $("#jqGrid").Grid({
        url: '../prostyle/list',
        height: '700px',
        colModel: [
            {label: '款式编号', name: 'id', index: 'id', key: true, width: 25, align: 'center'},
            {label: '产品编号', name: 'pid', index: 'pid', width: 25, align: 'center'},
            {label: '款式名称', name: 'name', index: 'name', width: 60, align: 'center'},
            {label: '价格', name: 'price', index: 'price', width: 25, align: 'center'},
            {label: '原价', name: 'oldPrice', index: 'old_price', width: 25, align: 'center'},
            {label: 'SPC价格', name: 'spcPrice', index: 'spcPrice', width: 25, align: 'center'},
            {label: '赠送能量', name: 'giveEnergy', index: 'giveEnergy', width: 25, align: 'center'},
            {label: '释放能量', name: 'spcFree', index: 'spcFree', width: 25, align: 'center'},
            {label: '团队奖励', name: 'giveTeam', index: 'giveTeam', width: 25, align: 'center'},
            {
                label: '款式图片', name: 'imageUrl', index: 'imageUrl', width: 60, align: 'center',
                formatter: function (value) {
                    if (value != "") {
                        return transImg(value);
                    } else {
                        return "无图片";
                    }
                }
            },
            {
                label: '规格', name: 'dimensions', index: 'dimensions', width: 150, align: 'center',
                formatter: function (value) {
                    if (typeof(value) != 'undefined' && value != null) {
                        let arr = JSON.parse(value);
                        let html = new Array();
                        html.push('<Table>');
                        for (var i = 0; i < arr.length; i++) {
                            html.push('<tr><td>' + JSON.stringify(arr[i]) + '</td></tr>');
                        }
                        html.push('</Table>');
                        return html.join('');
                    }
                    else
                        return '';
                }
            },
            {label: '创建日期', name: 'createDate', index: 'create_date', width: 60, align: 'center'},
            {label: '备注', name: 'remark', index: 'remark', width: 60, align: 'center'}]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        proStyle: {},
        dname: '',
        dprice: '',
        dnumber: 0,
        dsort: 1,
        ddescribe: '',
        dimensions: [],
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            pid: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.proStyle = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.proStyle.id == null ? "../prostyle/save" : "../prostyle/update";
            vm.proStyle.dimensions = vm.dimensions;
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.proStyle),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../prostyle/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../prostyle/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.proStyle = r.proStyle;
                    if (typeof r.proStyle.dimensions != 'undefined' && r.proStyle.dimensions != null && r.proStyle.dimensions != '') {
                        vm.dimensions = JSON.parse(r.proStyle.dimensions);
                    }
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.dname = '';
            vm.dprice = '';
            vm.dnumber = 0;
            vm.dsort = 1;
            vm.ddescribe = '';
            vm.dimensions = [];
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'pid': vm.q.pid},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');

        },
        reloadSearch: function () {
            vm.q = {
                pid: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessProImage: function (res, file) {
            vm.proStyle.imageUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImageProImage: function () {
            var url = vm.proStyle.imageUrl;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },
        //删除规格
        delDimensions: function (index) {
            this.dimensions.splice(index, 1);
        },
        addDimensions: function () {
            if (vm.dname == null || vm.dname === '') {
                iview.Message.error("请填写规格名称");
                return;
            }
            if (vm.dnumber == null || vm.dnumber === '') {
                iview.Message.error("请填写此规格数量价格");
                return;
            }

            let pdimen = {
                "name": vm.dname,
                "price": vm.dprice,
                "number": vm.dnumber,
                "sort": vm.dsort,
                "describe": vm.ddescribe
            };
            console.log(vm.dimensions);
            vm.dimensions.push(pdimen);
            vm.dname = '';
            vm.dprice = '';
            vm.dnumber = 0;
            vm.dsort = 1;
            vm.ddescribe = '';
        }
    }
});